﻿@{
    Layout = "~/Views/Shared/_DialogLayout.cshtml";
}
@using TjZheng.Power.DataEntity;
@inherits CMSWebViewPage<IEnumerable<PowerInfo>>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false">
        <div>
            @CurrentButtonTool.Create()
        </div>
        <table style="width: 85%;">
            <tr>
                <td>
                    @Html.Hidden("PowerIds")
                    @Html.DropDownList("PowerInfoList", Model.Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.PowerName }), null, new { size = 4, style = "height: 320px; width: 200px;" })
                </td>
                <td>
                    <a onclick="UpTop();" class="easyui-linkbutton">置顶</a>
                    <br />
                    <br />
                    <a onclick="Up();" class="easyui-linkbutton">上移</a>
                    <br />
                    <br />
                    <a onclick="Down();" class="easyui-linkbutton">下移</a>
                    <br />
                    <br />
                    <a onclick="DownBottom();" class="easyui-linkbutton">置底</a>
                </td>
            </tr>
        </table>
    </div>
</div>
@section Bottom{
    <script type="text/javascript">
        //初始化
        var objSelect = document.getElementById("PowerInfoList")
        var SortedIDObj = document.getElementById("PowerIds")

        //移至最顶
        function UpTop() {
            var nowIndex = objSelect.selectedIndex;
            if ((objSelect.value != "") && (nowIndex != 0)) {
                tempValue = objSelect.item(nowIndex).value;
                tempName = objSelect.item(nowIndex).text;
                for (i = nowIndex; i > 0; i--) {
                    objSelect.item(i).value = objSelect.item(i - 1).value;
                    objSelect.item(i).text = objSelect.item(i - 1).text;
                }
                objSelect.item(0).value = tempValue;
                objSelect.item(0).text = tempName;
                objSelect.item(0).selected = true;
            }
            ToSortIndex();
        }

        //移至最底
        function DownBottom() {
            var nowIndex = objSelect.selectedIndex;
            if ((objSelect.value != "") && (nowIndex != objSelect.length - 1)) {
                tempValue = objSelect.item(nowIndex).value;
                tempName = objSelect.item(nowIndex).text;
                for (i = nowIndex; i < objSelect.length - 1; i++) {
                    objSelect.item(i).value = objSelect.item(i + 1).value;
                    objSelect.item(i).text = objSelect.item(i + 1).text;
                }
                objSelect.item(objSelect.length - 1).value = tempValue;
                objSelect.item(objSelect.length - 1).text = tempName;
                objSelect.item(objSelect.length - 1).selected = true;
            }
            ToSortIndex();
        }

        //下移
        function Down() {
            var nowIndex = objSelect.selectedIndex;
            if ((objSelect.value != "") && (nowIndex != objSelect.length - 1)) {
                tempValue = objSelect.item(nowIndex).value;
                tempName = objSelect.item(nowIndex).text;
                objSelect.item(nowIndex).value = objSelect.item(nowIndex + 1).value;
                objSelect.item(nowIndex).text = objSelect.item(nowIndex + 1).text;
                objSelect.item(nowIndex + 1).value = tempValue;
                objSelect.item(nowIndex + 1).text = tempName;
                objSelect.item(nowIndex + 1).selected = true;
            }
            ToSortIndex();
        }

        //上移
        function Up() {
            var nowIndex = objSelect.selectedIndex;
            if ((objSelect.value != "") && (nowIndex != 0)) {
                tempValue = objSelect.item(nowIndex - 1).value;
                tempName = objSelect.item(nowIndex - 1).text;
                objSelect.item(nowIndex - 1).value = objSelect.item(nowIndex).value;
                objSelect.item(nowIndex - 1).text = objSelect.item(nowIndex).text;
                objSelect.item(nowIndex).value = tempValue;
                objSelect.item(nowIndex).text = tempName;
                objSelect.item(nowIndex - 1).selected = true;
            }
            ToSortIndex();
        }

        //生成排序字符串
        function ToSortIndex() {
            var SortTreeID = "";
            for (i = 0; i < objSelect.length; i++) {
                SortTreeID = SortTreeID + objSelect.item(i).value + ","
            }
            SortTreeID = SortTreeID.substring(0, SortTreeID.length - 1);
            SortedIDObj.value = SortTreeID;
        }

        function Save() {
            if (!SortedIDObj.value || SortedIDObj.value == "") {
                ShowMessage("请选择排序");
                return;
            }
            var objAjaxEntity = new AjaxEntity();
            objAjaxEntity.url = "/PowerInfo/SaveSort";
            objAjaxEntity.data = { powerIds: SortedIDObj.value };
            objAjaxEntity.executing = function () {
                ShowProcess();
                return true;
            };
            objAjaxEntity.executeSuccess = function (InvokeResult) {
                SetTargetItem("@GetString("dialog")", InvokeResult, window.parent);
                CloseProcess();
                CloseiFrameDialog("#@GetString("dialog")", window.parent);
            };
            objAjaxEntity.execute();
            //alert(SortedIDObj.value);
        }

        function Close() {
            CloseiFrameDialog("#@GetString("dialog")", window.parent);
        }
    </script>
}
